#!/usr/bin/env python

import gtk
import shutil
import os
import gettext

gettext.install('blendIT','locale');

class signals:

   def __init__(self,blend):
      self.blend = blend;

   def on_send_file_ok(self,widget,response,jid,descWidget):
      # Accept
      if response == gtk.RESPONSE_ACCEPT:
         fName = os.path.join(widget.get_current_folder(),widget.get_preview_filename());

         desc = descWidget.get_text();
         self.blend.mxitmessage.sendFile(jid,desc.strip(),fName);

      widget.destroy();

   def on_save_file_ok(self,widget,response,data,p=None):
      if response == gtk.RESPONSE_ACCEPT:
         target = os.path.join(widget.get_current_folder(),widget.get_filename());
         jid = data[0];
         file = data[1];
         source = os.path.join(self.blend.temp,file.name);
         
         # Copy the temp file to the location
         try:
            shutil.move(source,target);
         except Error:
            # Same file, shouldn't happen but
            # if it does it's not really a problem. Just pass
            pass;
         except IOError:
            # Couldn't copy. This may be a problem...
            self.blend.gui.createErrorMessage(dialog,_("Could not write to %s") % str(target));
            dialog.run();
         
         # Invalidate the file ID
         self.blend.conn.roster._files.pop(file.id);
            
         # Now we have to let the user know it's saved
         more = "";
         if source.lower().endswith("mxm"):
            more = ' | <a href="blendit://read:%s">Read Chats</a>' % target;
         self.blend.gui.chatWindow.systemMessage(_('Saved file %(name)s. <a href="blendit://view:%(path)s">View</a>%(end)s') % {'name': str(file.name), 'path': target, 'end': more},spawn=True,jids=[jid]);
         
      widget.destroy();

   def on_selection_change(self,widget):
      uri = widget.get_preview_filename();

      if uri == None:
         uri = "";
      # Try display it is an image if it's .png, .jpg, .jpeg or .gif
      if uri.split(".")[-1].lower() in ('jpg','jpeg','png','gif'):
         imageWidget = widget.get_preview_widget();
         imageWidget.set_from_file(uri);
         widget.set_preview_widget_active(True);
      else:
         widget.set_preview_widget_active(False);
         
   def on_close(self,widget):
      widget.destroy();